package com.atguigu.gmall.item.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author atguigu-mqx
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //  http://activity.gmall.com/seckill.html
    @GetMapping("seckill.html")
    public String seckillList(Model model){
        //  获取数据
        Result result = activityFeignClient.findAll();
        //  存储数据
        model.addAttribute("list",result.getData());
        //  返回秒杀列表页面
        return "seckill/index";
    }

    //  http://activity.gmall.com/seckill/19.html
    @GetMapping("seckill/{skuId}.html")
    public String seckillSkuId(@PathVariable Long skuId,Model model){
        //  获取数据
        Result result = activityFeignClient.getSeckillGoods(skuId);
        //  存储数据
        model.addAttribute("item",result.getData());
        //  返回页面
        return "seckill/item";
    }

    //  排队页面
    //  window.location.href = '/seckill/queue.html?skuId='+this.skuId+'&skuIdStr='+skuIdStr
    @GetMapping("seckill/queue.html")
    public String seckillQueue(HttpServletRequest request){
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));
        //  返回页面
        return "seckill/queue";
    }

    //  去下单页面
    @GetMapping("seckill/trade.html")
    public String seckillTrade(Model model){
        //  获取远程调用数据
        Result<Map<String, Object>> result =  activityFeignClient.trade();
        if (result.isOk()){
            //  userAddressList, detailArrayList totalNum totalAmount
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        }else {
            model.addAttribute("message","web-all中下单失败!");
            return "seckill/fail";
        }


    }


}
